package com.example.demo.service;


import com.example.demo.pojo.Comment;
import com.example.demo.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CommentService {

    @Autowired
    private CommentRepository commentRepository;

    // 保存评论
    public Comment saveComment(String username, String content) {
        Comment comment = new Comment(username, content, LocalDateTime.now());
        return commentRepository.save(comment);
    }

    // 分页查询评论
    public Page<Comment> getComments(Pageable pageable) {
        return commentRepository.findAllByOrderByCreatedAtDesc(pageable);
    }
}